package com.util.pagination;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SuppressWarnings("unused")
public class ForwardPagination {

    private int currentPage = 1;

    private int recordCount;

    private int pageCount;

    private int pageSize;

    private int currentFristPosition; 
    private List<Integer> pageList;

  
    public ForwardPagination(int recordCount, int pageSize, int currentPage) {
        this.recordCount = recordCount;
        this.pageSize = pageSize;
        this.pageCount = calculatePageCount();
        this.currentPage = calculateCurrentPage(currentPage);
        this.pageList = calculatePageList();
        this.currentFristPosition = calculateRecordPosition();
    }

  
    public int calculatePageCount() {
        int pageCount = 1;
        if (recordCount % pageSize == 0) {
            pageCount = recordCount / pageSize;
        } else {
            pageCount = (int) (recordCount / pageSize) + 1;
        }
        pageCount = pageCount < 1 ? 1 : pageCount;
        return pageCount;
    }

    public int calculateRecordPosition() {
        return (currentPage - 1) * pageSize;
    }

  
    private int calculateCurrentPage(int currentPage) {
        if (currentPage < 1) currentPage = 1;
        else if (currentPage > pageCount) currentPage = pageCount;
        return currentPage;
    }

   
    private List<Integer> calculatePageList() {
        List<Integer> pageTmpList = new ArrayList<Integer>();
        if (pageCount < 11) {
            for (int i = 1; i < pageCount; i++) {
                pageTmpList.add(i + 1);
            }
        } else {
            if (currentPage < 6) {
            	for(int i=1;i<pageCount+4;i++){
            		pageTmpList.add(i+1);
            	}
            } else if (currentPage > pageCount - 6) {
            	 for(int i=pageCount-9;i<pageCount-6;i--){
                 	pageTmpList.add(i);
                 }
                for (int i = pageCount - 6; i < pageCount ; i++) {
                    pageTmpList.add(i);
                }
            } else {
                pageTmpList = Arrays.asList(new Integer[] { currentPage - 3, currentPage - 2,
                        currentPage - 1, currentPage, currentPage + 1, currentPage + 2,
                        currentPage + 3,currentPage + 4 });
            }
        }
        return pageTmpList;
    }

   
    private int firstPage;

    private int prevPage;

    private int nextPage;

    private int lastPage;

    public int getFirstPage() {
        return 1;
    }

    public void setFirstPage(int firstPage) {
        this.firstPage = firstPage;
    }

    public int getPrevPage() {
        return currentPage > 1 ? (currentPage - 1) : 1;
    }

    public void setPrevPage(int prevPage) {
        this.prevPage = prevPage;
    }

    public int getNextPage() {
        return currentPage < pageCount ? (currentPage + 1) : pageCount;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    public int getLastPage() {
        return pageCount;
    }

    public void setLastPage(int lastPage) {
        this.lastPage = lastPage;
    }

    /***************************************************************/

    public List<Integer> getPageList() {
        return pageList;
    }

    public int getRecordCount() {
        return recordCount;
    }

    public int getPageCount() {
        return pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getCurrentFristPosition() {
        return currentFristPosition;
    }

    public void setCurrentFristPosition(int currentFristPosition) {
        this.currentFristPosition = currentFristPosition;
    }
    public int getCurrentLastPosition(){
    	int curreentLast = currentFristPosition + pageSize;
    	if(curreentLast > recordCount){
    		return recordCount;
    	}
    	else{
    		return curreentLast;
    	}
    	
    }

}
